/*
 *  ifxdriver.cpp - Informix dbExpress driver implementation
 *
 *    Copyright (c) 2001-2006, Luxena Software Company. All rights reserved.
 *
 *  Purpose:
 *
 */

#include "ifxdriver.hpp"

#include <new>
#include "dbxcommon.hpp"
#include "ifxconnection.hpp"


/////////////////////////////////////////////////////////////////////////////
// API implementation

extern "C" SQLResult API_CALL_TYPE getInformixDriver(
	pCHAR pszVendorLib, pCHAR pszResourceFile, ppSQLDriver ppDrv)
{
	try
	{
		*ppDrv = new CInformixDriver;
	}
	catch (std::bad_alloc&)
	{
		return DBXERR_NOMEMORY;
	}

	return SQL_SUCCESS;
}


/////////////////////////////////////////////////////////////////////////////
// CInformixDriver - SQLDriver implementation for Informix

CInformixDriver::CInformixDriver ()
{
	_blobSize = -1;
}

int4 CInformixDriver::getBlobSize ()
{
	return _blobSize;
}

CCallback* CInformixDriver::getCallback()
{
	if (_callback.getHandler() == 0)
		return 0;

	return &_callback;
}

SQL_METHOD_IMP CInformixDriver::getSQLConnection (ppSQLConnection ppConn)
{
	try
	{
		*ppConn = new CInformixConnection(this);
	}
	catch (std::bad_alloc&)
	{
		return DBXERR_NOMEMORY;
	}

	return SQL_SUCCESS;
}

SQL_METHOD_IMP CInformixDriver::setOption (eSQLDriverOption eDOption, INT32 PropValue)
{
	switch (eDOption)
	{
	case eDrvBlobSize:
		_blobSize = PropValue; break;

	case eDrvCallBack:
		_callback.setHandler(reinterpret_cast<pfCallBack>(PropValue)); break;

	case eDrvCallBackInfo:
		_callback.setInfo(PropValue); break;

	case eDrvRestrict:
		return SQL_SUCCESS;

	default:
		return DBXERR_NOTSUPPORTED;
	}

	return SQL_SUCCESS;
}

SQL_METHOD_IMP CInformixDriver::getOption (eSQLDriverOption eDOption, pINT32 plValue, INT16 iMaxLength, pINT16 piLength)
{
	try
	{
		switch (eDOption)
		{
		case eDrvBlobSize:
			*getVerifiedValueDestination(sizeof(INT32), plValue, iMaxLength, piLength) = _blobSize; break;

		case eDrvCallBack:
			*getVerifiedValueDestination(sizeof(INT32), plValue, iMaxLength, piLength) = reinterpret_cast<INT32>(_callback.getHandler()); break;

		case eDrvCallBackInfo:
			*getVerifiedValueDestination(sizeof(INT32), plValue, iMaxLength, piLength) = _callback.getInfo(); break;

		case eDrvRestrict:
			*piLength = 0; break;

		default:
			return DBXERR_NOTSUPPORTED;
		}
	}
	catch (SQLResult sqlResult)
	{
		return sqlResult;
	}

	return SQL_SUCCESS;
}
